<template>
  <div class="edit">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>{{title}}</span>
        <el-button @click="$router.go(-1)" style="float: right; padding: 3px 0" type="text">返回</el-button>
      </div>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="年级名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入年级名称" />
        </el-form-item>
        <el-form-item label="科目" prop="subject">
          <el-select
            style="width:100%"
            multiple
            filterable
            allow-create
            v-model="form.subject"
            placeholder="请选择（多选）或输入科目"
          >
            <el-option label="语文" value="语文"></el-option>
            <el-option label="数学" value="数学"></el-option>
            <el-option label="英语" value="英语"></el-option>
            <el-option label="物理" value="物理"></el-option>
            <el-option label="化学" value="化学"></el-option>
            <el-option label="生物" value="生物"></el-option>
            <el-option label="政治" value="政治"></el-option>
            <el-option label="历史" value="历史"></el-option>
            <el-option label="地理" value="地理"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm">确定</el-button>
          <el-button @click="cancel">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import Upload from "@/components/Upload";
import { getClass, addClass, updateClass } from "@/api/system/class";

export default {
  name: "ClassEdit",
  components: {
    Upload
  },
  data() {
    return {
      title: "添加年级与科目",
      // 表单参数
      form: {
        name: "",
        subject: []
      },
      // 表单校验
      rules: {
        subject: [{ required: true, message: "请选择科目" }],
        name: [{ required: true, message: "请输入年级名称", trigger: "blur" }]
      }
    };
  },
  mounted() {
    this.getEditInfo();
  },
  methods: {
    clearImageValidate() {
      this.rules.imageUrl = [];
      this.$refs["form"].clearValidate("imageUrl");
    },
    getEditInfo() {
      const id = this.$route.query.id;
      if (id) {
        getClass(id).then(response => {
          this.form = {
            ...response.data,
            subject: response.data.subject.split(",")
          };
        });
        this.title = "修改年级与科目";
      }
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            const postData = {
              ...this.form,
              subject: this.form.subject.join(",")
            };
            updateClass(postData).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.$router.go(-1);
              }
            });
          } else {
            const postData = {
              ...this.form,
              subject: this.form.subject.join(",")
            };
            addClass(postData).then(response => {
              if (response.code === 200) {
                this.msgSuccess("添加成功");
                this.$router.go(-1);
              }
            });
          }
        }
      });
    },
    // 重置按钮
    cancel() {
      this.getEditInfo();
      this.resetForm("form");
    }
  }
};
</script>
